const fs = require("fs");
const path = require("path");
const postcss = require("postcss");
const tailwind = require("tailwindcss");
const OPTIONS = require("../lib/config");

const filename = "tailwind";
const args = process.argv.slice(2);
const platform = args[0] || "web";
const config = path.join(__dirname, `../tailwind.config.${platform}.js`);
const inputFile = path.join(__dirname, `../${filename}.src.css`);
const outputFile = path.join(__dirname, `../dist/${filename}.css`);

fs.readFile(inputFile, (err, css) => {
    if (err) throw err;
    console.log(`platform: ${platform}`);
    postcss([
        tailwind(config),
        require("../lib/main")({
            platform: platform,
            name: args[1] || "",
            debug: true,
        })
    ])
        .process(css, {
            from: inputFile,
            to: outputFile,
            map: {inline: false}
        })
        .then(result => {
            fs.writeFileSync(outputFile, result.css, "utf8");
            if (result.map) {
                fs.writeFileSync(
                    `${outputFile}.map`,
                    JSON.stringify(result.map),
                    "utf8"
                );
            }
            return result;
        })
        .then(result => {
            console.log("Built.");
        });
});
